package com.qf.smsplatform.search.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.smsplatform.search.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ElasticsearchService elasticsearchService;


    @RequestMapping(value = "/search/searchcount", method = RequestMethod.POST)
    public Long searchLogCount(@RequestParam("params") String params) throws IOException {
        //1. 将params手动的反序列化为Map对象
        Map parameter = objectMapper.readValue(params, Map.class);

        //2. 调用service查询数据总条数
        Long count = elasticsearchService.searchLogCount(parameter);

        //3. 返回
        return count;
    }

    @RequestMapping(value = "/search/search", method = RequestMethod.POST)
    public List<Map> searchLog(@RequestParam("params") String params) throws IOException {
        //1. 将params手动的反序列化为Map对象
        Map parameter = objectMapper.readValue(params, Map.class);

        //2. 调用service查询数据总条数
        List<Map> data = elasticsearchService.searchLog(parameter);

        //3. 返回
        return data;
    }



    @RequestMapping(value = "/search/statStatus", method = RequestMethod.POST)
    public Map<String, Long> statSendStatus(@RequestParam("params") String params) throws IOException {
        //1. 将params手动的反序列化为Map对象
        Map parameter = objectMapper.readValue(params, Map.class);

        //2. 调用service查询数据总条数
        Map<String, Long> data = elasticsearchService.statSendStatus(parameter);

        //3. 返回
        return data;
    }

}
